home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / xinetd.2 / xinetd / xinetd.2.1.7-linux.4 / libs / src / str / strparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-25  |  4.6 KB  |  220 lines

  1. /*
  2.  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: strparse.c,v 3.1 1993/06/13 02:48:19 panos Exp $" ;
  8. static char version[] = VERSION ;
  9.  
  10. #include "str.h"
  11. #include "strparse.h"
  12.  
  13. char *strcpy() ;
  14. char *strncpy() ;
  15. char *strpbrk() ;
  16.  
  17. char *malloc() ;
  18.  
  19. int str_errno ;
  20.  
  21.  
  22. PRIVATE char *new_string( s )
  23.     register char *s ;
  24. {
  25.     register char *p = malloc( (unsigned)strlen( s ) + 1 ) ;
  26.  
  27.     return( p ? strcpy( p, s ) : p ) ;
  28. }
  29.  
  30.  
  31. str_h str_parse( str, separ, flags, errnop )
  32.     register char    *str ;
  33.     char                *separ ;
  34.     int                flags ;
  35.     int                *errnop ;
  36. {
  37.     register struct str_handle *hp ;
  38.     int *errp = ( errnop == NULL ) ? &str_errno : errnop ;
  39.  
  40.     if ( separ == NULL )
  41.         HANDLE_ERROR( flags, NULL, errp, STR_ENULLSEPAR,
  42.                                 "STR str_parse: NULL separator\n" ) ;
  43.  
  44.     hp = (struct str_handle *) malloc( sizeof( struct str_handle ) ) ;
  45.     if ( hp == NULL )
  46.         HANDLE_ERROR( flags, NULL, errp, STR_ENOMEM,
  47.                                 "STR str_parse: malloc failed\n" ) ;
  48.  
  49.     hp->string = str ;
  50.     hp->pos = str ;
  51.     hp->separator = new_string( separ ) ;
  52.     if ( hp->separator == NULL )
  53.         if ( flags & STR_RETURN_ERROR )
  54.         {
  55.             free( (char *) hp ) ;
  56.             *errp = STR_ENOMEM ;
  57.             return( NULL ) ;
  58.         }
  59.         else
  60.             TERMINATE( "STR str_parse: malloc failed\n" ) ;
  61.     
  62.     hp->flags = flags ;
  63.     hp->errnop = errp ;
  64.     hp->no_more = ( str == NULL ) ;
  65.     return( (str_h) hp ) ;
  66. }
  67.  
  68.  
  69. void str_endparse( handle )
  70.     str_h handle ;
  71. {
  72.     register struct str_handle *hp = (struct str_handle *) handle ;
  73.  
  74.     free( hp->separator ) ;
  75.     free( (char *) handle ) ;
  76. }
  77.  
  78.  
  79. /*
  80.  * Change the string
  81.  */
  82. int str_setstr( handle, newstr )
  83.     str_h handle ;
  84.     char *newstr ;
  85. {
  86.     register struct str_handle *hp = (struct str_handle *) handle ;
  87.     
  88.     if ( newstr == NULL )
  89.         HANDLE_ERROR( hp->flags, STR_ERR, hp->errnop, STR_ENULLSTRING,
  90.                                 "STR str_setstr: NULL string\n" ) ;
  91.     
  92.     hp->string = newstr ;
  93.     hp->pos = newstr ;
  94.     hp->no_more = FALSE ;
  95.     return( STR_OK ) ;
  96. }
  97.  
  98.  
  99.  
  100. /*
  101.  * Change the separator
  102.  */
  103. int str_separator( handle, separator )
  104.     str_h handle ;
  105.     char *separator ;
  106. {
  107.     register struct str_handle *hp = (struct str_handle *) handle ;
  108.     char *new_separator ;
  109.  
  110.     if ( separator == NULL )
  111.         HANDLE_ERROR( hp->flags, STR_ERR, hp->errnop, STR_ENULLSEPAR,
  112.                                 "STR str_separator: NULL separator\n" ) ;
  113.     new_separator = new_string( separator ) ;
  114.     if ( new_separator == NULL )
  115.         HANDLE_ERROR( hp->flags, STR_ERR, hp->errnop, STR_ENOMEM,
  116.             "STR str_separator: malloc failed\n" ) ;
  117.  
  118.     free( hp->separator ) ;
  119.     hp->separator = new_separator ;
  120.     return( STR_OK ) ;
  121. }
  122.  
  123.  
  124. char *str_nextpos( handle )
  125.     str_h handle ;
  126. {
  127.     struct str_handle *hp = (struct str_handle *) handle ;
  128.  
  129.     if ( hp->no_more || *hp->pos == '\0' )
  130.         return( NULL ) ;
  131.     else
  132.         return( hp->pos ) ;
  133. }
  134.  
  135.  
  136. char *str_component( handle )
  137.     str_h handle ;
  138. {
  139.     register char                        *start ;
  140.     register char                        *last ;
  141.     register int                        sep_count ;
  142.     char                                    *retval ;
  143.     int                                    last_char ;
  144.     register struct str_handle        *hp            = (struct str_handle *) handle ;
  145.     register int                        first_call    = ( hp->pos == hp->string ) ;
  146.  
  147.     if ( hp->no_more )
  148.         return( NULL ) ;
  149.  
  150.     /*
  151.      * Get number of separator characters.
  152.      * Find beginning of component.
  153.      */
  154.     sep_count = strspn( hp->pos, hp->separator ) ;
  155.  
  156.     /*
  157.      * If this is the first call, and there are separator characters
  158.      * at the beginning of the string and the STR_NULL_START flag is set
  159.      * we return a 0-length string.
  160.      */
  161.     if ( first_call && sep_count > 0 && ( hp->flags & STR_NULL_START ) )
  162.     {
  163.         start = hp->pos ;
  164.         last = hp->pos ;
  165.     }
  166.     else
  167.     {
  168.         start = hp->pos + sep_count ;
  169.  
  170.         if ( *start == '\0' )
  171.         {
  172.             last = start ;
  173.             hp->no_more = TRUE ;
  174.             if ( ! ( hp->flags & STR_NULL_END ) )
  175.                 return( NULL ) ;
  176.         }
  177.         else
  178.         {
  179.             last = strpbrk( start, hp->separator ) ;
  180.             if ( last == NULL )
  181.                 last = start + strlen( start ) ;
  182.         }
  183.     }
  184.  
  185.     /*
  186.      * At this point, the following variables must be set:
  187.      *        start:    beginning of component
  188.      *        last:       end of component + 1
  189.      *
  190.      * If STR_MALLOC is set, allocate space for the new string.
  191.      *
  192.      * NOTE: If STR_MALLOC is not set, the processed string is trashed.
  193.      */
  194.     last_char = *last ;
  195.     if ( hp->flags & STR_MALLOC )
  196.     {
  197.         int len = last - start ;
  198.  
  199.         retval = malloc( (unsigned)len + 1 ) ;
  200.         if ( retval == NULL )
  201.             HANDLE_ERROR( hp->flags, NULL, hp->errnop, STR_ENOMEM,
  202.                                             "STR str_component: malloc failed\n" ) ;
  203.         strncpy( retval, start, len )[ len ] = '\0' ;
  204.     }
  205.     else
  206.     {
  207.         retval = start ;
  208.         *last = '\0' ;
  209.     }
  210.  
  211.     /*
  212.      * Check if last_char is NUL to avoid setting hp->pos past the
  213.      * end of the string
  214.      */
  215.     hp->pos = ( last_char == '\0' ) ? last : last+1 ;
  216.     return( retval ) ;
  217. }
  218.  
  219.  
  220.